<?php
/**
 * 媒资管理上传
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\mediamanage;
use app\api\controller\common\ApiBase;
use app\api\service\Uploader;
use think\Db;
class Upload extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companymgr');
    }


    public function upload()
    {
        $up = new Uploader();
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method === 'POST') { //上传
            $up->fileInfo = [
                'identifier' => htmlentities($_POST['identifier']), //每个文件的唯一标识
                'filename' => htmlentities($_POST['filename']), //文件名称
                'totalSize' => intval($_POST['totalSize']), //文件总大小
                'chunkNumber' => intval($_POST['chunkNumber']), //当前是第几个分片
                'totalChunks' => intval($_POST['totalChunks']) //总分片数
            ];
            $res = $up->upload();
        } else { //上传前检测文件md5和分片
            $res = $up->checkFile();
        }
        return json($res);
    }

    public function merge() {

        $up = new Uploader();
        $post = file_get_contents('php://input');
        $data = json_decode($post, true);
        $up->fileInfo = [
            'filename' => htmlentities($data['filename']), //文件名称
            'identifier' => htmlentities($data['identifier']), //文件唯一标识
            'totalSize' => intval($data['totalSize']), //文件总大小
            'totalChunks' => intval($data['totalChunks']) //总分片数
        ];
        $res = $up->merge();
        return json($res);
    }


}